package com.crl.module.live
{
	import com.crl.module.live.events.NetStreamEvent;
	import com.crl.utils.Delegate;
	import com.crl.utils.FMSUtil;
	import com.crl.utils.StringUtil;
	
	import flash.events.NetStatusEvent;
	import flash.media.Video;
	import flash.net.NetStream;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	 * 接受视频服务;
	 * 
	 * 当开始播放,关闭时,分别记录isRunning值, 
	 * 播放时侦听流及网络状态,只在要此过程中才进行网络重连;
	 * 
	 * @author crl;
	 * 
	 */	
	public class Receiver extends AbstractLive
	{		
		private var playName:String="";
		public function Receiver(singleLive:SingleLive)
		{
			super(singleLive);
			
		}
		
		/**
		 * 是否接收视频; 
		 * @param bool
		 * 
		 */		
		override public function set videoStatus(bool:Boolean) : void{
			super.videoStatus=bool;
			if(stream)
				stream.receiveVideo(bool);
			
			//顺便关掉视频窗口;
			var list:Array=this.singleLive.getVideos();
			for each(var vi:Video in list){
				vi.visible=bool;
			}
		}
		
		
		/**
		 * 播放一个远程流; 
		 * @param name
		 * 
		 */		
		public function play(name:String):void{
			clearTimeout(timeoutResetToLiveInterval);
			
			if(this.singleLive.sharerConnection.isConnected==false){
        		this.singleLive.sharerConnection.doConnection(Delegate.create(play,name));
        		return;
        	}
        	
        	//将要播放的为当前的音乐,不做外理;
        	if(name==this.playName){
        		return;
        	}
        	
        	conn=singleLive.sharerConnection.getConnection();
			
			//如果有旧流存在;
			if(stream){
        		stream.removeEventListener(NetStatusEvent.NET_STATUS,status);
        		stream.close();
			}
			
        	stream = FMSUtil.createNetStream(conn,stream);
        	
        	if(stream==null){
        		return;
        	}
        	
        	stream.client=client;
        	stream.addEventListener(NetStatusEvent.NET_STATUS,status);
        	
        	this.singleLive.showVideos(stream);
        	this.isRunning=true;
        	
        	try
            {
                this.stream.play(name);
                this.playName=name;
                this.conn.addEventListener(NetStatusEvent.NET_STATUS,status);
                
                this.singleLive.sharerConnection.getReferences().put(this,this);
            }
            catch (e:Error)
            {
            	var msg:String=StringUtil.substitute("播放视频时出错,流名称: {0} ,{1}" ,name,e.message);
                trace(msg);
            }
		}
		
		/**
		 * 有元数据变化时; 
		 * @param info
		 * 
		 */		
		override public function onMetaData(info:Object):void{
			this.singleLive.dispatchEvent(new NetStreamEvent(NetStreamEvent.ONMETADATA,info));
		}
		
		
		/**
		 * 远程状态值,当前主要帧听断线及,流发布事情; 
		 * @param event
		 * 
		 */		
		override protected function status(event:NetStatusEvent):void{
			switch (event.info.code) {
				case LiveConst.NOFOUND :
					trace("stream not found");
					break;
				case LiveConst.STOP :
					break;
				case LiveConst.START :
					break;
				case LiveConst.FULL :
					break;
				case LiveConst.EMPTY :
					break ;
				case LiveConst.PUBLISHNOTIFY :
					break;
				case LiveConst.UNPUBLISHNOTIFY :
				
					var preStream:NetStream=event.target as NetStream;
					preStream.removeEventListener(NetStatusEvent.NET_STATUS,status);
					
					///断开流是否为当前流,如果有新流创建旧流肯定删除,但如果没有新流的话,那我们这边就要处理掉旧的流;
					if(preStream==stream){
						this.close();
					}else{
						preStream.close();
					}		
					break ;
				case LiveConst.CLOSED :
					replayCallLater(this.playName);
					break;
				default :
					break;
			}
		}
		
		private function replayCallLater(name:String):void{
			if(name !=""){
				clearTimeout(timeoutResetToLiveInterval);
				
				/**
				 * 其中有可能只有我为掉线用户,也有可能所有人掉线.
				 * */
				timeoutResetToLiveInterval=setTimeout(this.preRePlay,LiveConst.RECEIVER_RECONN_TICK,name);
			}
		}
		
		/**
		 *	是否在要重试之前已经连上,如已经连上.不去播放; 
		 * @param name
		 * 
		*/	
		private function preRePlay(name:String):void{
			if(this.singleLive.sharerConnection.isConnected)
			{
				return;
			}
			
			//而且名称没有变掉;
			if(name==this.playName){
				//HelpTipsPanel.show("视频服务断线.系统自动进行重新接收",HelpTipsPanel.ICON_SOCIAL);
				play(name);
			}
		} 
		

		override public function close():void{
			if(stream){
				stream.removeEventListener(NetStatusEvent.NET_STATUS,status);
				stream=null;
			}
			if(conn){
				conn.removeEventListener(NetStatusEvent.NET_STATUS,status);
				conn=null;
			}
			this.playName="";
			super.close();
			
		}
	}
}